package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.mappers.ShopTempateMapper;
import com.burgeon.backend.SHOP.service.EmpNotificationMessageService;
import com.burgeon.backend.WeChat.WeChatMessageConnector;
import com.burgeon.backend.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmpNotificationMessageServiceImpl  implements EmpNotificationMessageService {
    @Autowired
    private WechatService wechatService;

    @Autowired
    private WeChatMessageConnector weChatMessageConnector;

    @Autowired
    private ShopTempateMapper shopTempateMapper;
    //超时
    @Override
    public void sendEmpOvertime( String openid, String token) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId =shopTempateMapper.findTemplateSwitch4(token) ;
        String value1 = "店铺未能履约发货,请及时关注店铺发货";
        String value2 = "店铺超时发货";
        String value3 = "待跟进";
        String name = shopTempateMapper.findTemplateName(token);
        String data = sendMessageStore( openid, templateId,value1,value2,value3,name);


        String str = weChatMessageConnector.Send(url, data);
        System.err.println("订单超时发送情况："+str);
    }

//积分兑换
    @Override
    public void sendEmpCreditsExchange(String empName, String vipName, String vipPhone, String openid, String orderId, String token) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId = shopTempateMapper.findTemplateSwitch4(token);
        String value1 = "积分商城收到顾客积分兑换的订单,请及时处理";
        String value2 = "积分兑换";
        String value3 = "待处理";
        String data = sendMessage(empName, vipName, vipPhone, openid, orderId, templateId,value1,value2,value3);

        String str = weChatMessageConnector.Send(url, data);
        System.err.println(str);
    }
    //手动分单
    @Override
    public void sendEmpDelivery(String openid, String token) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId = shopTempateMapper.findTemplateSwitch4(token);
        String value1 = "存在订单未能由云仓自动派单,请及时处理";
        String value2 = "手工分单";
        String value3 = "待处理";
        String name = shopTempateMapper.findTemplateName(token);
        String data = sendMessageStore( openid, templateId,value1,value2,value3,name);

        String str = weChatMessageConnector.Send(url, data);
        System.err.println("手工分单发送状态"+str);
    }


    //退款
    @Override
    public void sendEmpRefund(String empName, String vipName, String vipPhone, String openid, String orderId, String token) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId = shopTempateMapper.findTemplateSwitch4(token);
        String value1 = "云微平台收到顾客的退款申请,请及时处理";
        String value2 = "退货通知";
        String value3 = "待处理";
        String data = sendMessage(empName, vipName, vipPhone, openid, orderId, templateId,value1,value2,value3);

        String str = weChatMessageConnector.Send(url, data);
        System.err.println(str);
    }

    //新订单
    @Override
    public void sendEmpNewOrder(String empName, String vipName, String vipPhone, String openid, String orderId, String token) {
        String accessToken = wechatService.GetAccesTokenByAccount(token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        String templateId =shopTempateMapper.findTemplateSwitch4(token);
        String value1 = "云微平台收到新的商品订单,请及时处理";
        String value2 = "订单通知";
        String value3 = "待处理";
        String data = sendMessage(empName, vipName, vipPhone, openid, orderId, templateId,value1,value2,value3);

        String str = weChatMessageConnector.Send(url, data);
        System.err.println(str);

    }


    private String sendMessageStore(String openid, String templateId,
                               String value1,String value2,String value3,String name){
        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + templateId + "\",\n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\""+value1+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\""+value2+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\""+value3+"\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" +
                "                       \"value\":\"" + "客服" + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword4\": {\n" +
                "                       \"value\":\"" + name + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\""+"进入系统 查看详情"+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";

    }
//    private String sendMessageCustomer(String empName, String customerName, String openid, String templateId,
//                                    String value1,String value2,String value3){
//        return "{  \"touser\":\"" + openid + "\",\n" +
//                "           \"template_id\":\"" + templateId + "\",\n" +
//                "           \"data\":{\n" +
//                "                   \"first\": {\n" +
//                "                       \"value\":\""+value1+"\",\n" +
//                "                       \"color\":\"#173177\"\n" +
//                "                   },\n" +
//                "                   \"keyword1\":{\n" +
//                "                       \"value\":\""+value2+"\",\n" +
//                "                       \"color\":\"#173177\"\n" +
//                "                   },\n" +
//                "                   \"keyword2\": {\n" +
//                "                       \"value\":\""+value3+"\",\n" +
//                "                        \"color\":\"#173177\"\n" +
//                "                   },\n" +
//                "                   \"keyword3\": {\n" +
//                "                       \"value\":\"" + "客服" + "\",\n" +
//                "                        \"color\":\"#173177\"\n" +
//                "                   },\n" +
//                "                   \"keyword4\": {\n" +
//                "                       \"value\":\"" + "厚德洪恩"+ "\",\n" +
//                "                        \"color\":\"#173177\"\n" +
//                "                   },\n" +
//                "                   \"remark\":{\n" +
//                "                       \"value\":\""+"进入系统 查看详情"+"\",\n" +
//                "                       \"color\":\"#173177\"\n" +
//                "                   } }   }";
//
//    }

    private String sendMessage(String empName, String vipName, String vipPhone, String openid, String orderId,String templateId,
    String value1,String value2,String value3){
        empName = "客服";
        return "{  \"touser\":\"" + openid + "\",\n" +
                "           \"template_id\":\"" + templateId + "\",\n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\""+value1+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\""+value2+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword2\": {\n" +
                "                       \"value\":\""+value3+"\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword3\": {\n" +
                "                       \"value\":\"" + vipName+vipPhone + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"keyword4\": {\n" +
                "                       \"value\":\"" + empName + "\",\n" +
                "                        \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\""+"订单号:"+orderId+" 手机号:"+vipPhone+"\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   } }   }";

    }
}
